package com.appspot.smartgan;

import java.util.List;

import com.appspot.smartgan.adapter.MessagesAdapter;
import com.appspot.smartgan.server.Client;
import com.appspot.smartgan.server.HttpClient;
import com.appspot.smartgan.server.UrlGenerator;
import com.appspot.smartgan.util.Message;
import com.twotoasters.jazzylistview.JazzyListView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class PrivateMessagesFragment extends Fragment {

	private static String id;
	private static String school;
	
	private List<Message> list;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {	
		View rootView = 
				inflater.inflate(R.layout.fragment_private_messages, container, false);
		
		// sort the list according to date
		ChildActivity activity = (ChildActivity) getActivity();
		activity.setList(list);
		
		// create list view
		JazzyListView listView = 
				(JazzyListView) rootView.findViewById(R.id.private_messages_list);
		ArrayAdapter<Message> adapter = new MessagesAdapter(getActivity(), list);
		listView.setAdapter(adapter);
		activity.setListAdapter(adapter);
		
		return rootView;
	}
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// get url and create connection
		String url = UrlGenerator.getPrivateMessages(id, school);
		Client client = new HttpClient(url, Client.RESPONSE_PRIVATE_MESSAGES);
		client.execute();
		
		list = (List<Message>) client.getResponse();
		if (list == null) {
			return;
		}
	}

	public void setChild(String id) {
		PrivateMessagesFragment.id = id;
	}

	public void setSchool(String id) {
		PrivateMessagesFragment.school = id;
	}
	
}
